package io.github.lee.jel.ui.item.play

import android.util.Log
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player

class PlayListener(private val vm: MediaPlayVM) : Player.Listener {
    override fun onPlaybackStateChanged(playbackState: Int) {
        super.onPlaybackStateChanged(playbackState)
        when (playbackState) {
            Player.STATE_IDLE -> Log.e("TAG", "idle")
            Player.STATE_BUFFERING -> vm.showProgress(canDismiss = false)
            Player.STATE_READY -> {
                vm.hideProgress()
                vm.player?.play()
                //更新按钮
                vm.isPlayingLiveData.postValue(vm.player?.isPlaying ?: false)
                //当前播放时长
                vm.progressHandle.sendEmptyMessage(1)
                //进度

            }

            Player.STATE_ENDED -> Log.e("TAG", "end")
        }
    }

    override fun onIsPlayingChanged(isPlaying: Boolean) {
        super.onIsPlayingChanged(isPlaying)
        if (isPlaying) {
//            hideProgress()
        }

    }

    override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
        super.onPlayWhenReadyChanged(playWhenReady, reason)
        Log.e("TAG", "onPlayWhenReadyChanged = $reason")

    }


    override fun onPlayerError(error: PlaybackException) {
        super.onPlayerError(error)
    }
}